
#include "FreeRTOS_physical.h"


static unsigned char in1_buf,in2_buf,in3_buf,sw_buf;
unsigned char outdir;


unsigned char get_button(unsigned char _input3,unsigned char button_mask){
	if(_input3 & (~button_mask))
		return 0;
	else
		return 1;
}

unsigned char get_sensor(unsigned char _input2,unsigned char mask1)
{
	if(_input2 &(~mask1))
		return 0;
	else return 1;
}

void init_physical(unsigned char *_input1,unsigned char *_input2, unsigned char *_input3, unsigned char *_input_dipsw)
{                                     
    *_input1 = *_input2 = *_input3 = NO_INPUT;
    *_input_dipsw = NO_INPUT;
	//init_sw()
	 // PB.5 PB.6 is a pull up resistors for switch 0 and 1       //
    PORTB |= (_BV(SW_0) | _BV(SW_1));
    DDRB &= (~(_BV(SW_0) | _BV(SW_1))); 
    // PG.3 PG.4 is a pull up resistors for switch 2 and 3
    PORTG |= (_BV(SW_2) | _BV(SW_3));
    DDRG &= (~(_BV(SW_2) | _BV(SW_3)));
}
//Funcion Implementation

void writeIO(unsigned char ID, unsigned char val)
{    
    PORTG   &=  ID;
    DDRA    =   OUTPUT_DIR;
    //PORTA   =   ~val; 
    PORTA   =   val;
    PORTG   =   ID; //delay 1 Instruction cycle
    PORTG   =  MASK_IO; 
    PORTG   =  MASK_IO;
}

/*
void updateOutput(void)  //call from timer ISR
{
    writeIO(OUTRESERVE,output);
    writeIO(OUTLED,outled);
    writeIO(OUTDIR,outdir);
}
*/

unsigned char readIO(unsigned char ID)
{
    unsigned char temp;
    PORTG   &=  ID;
    DDRA    =   INPUT_DIR;//input
    temp    =   PINA;
    temp    =   PINA;
    PORTG   =  MASK_IO;
    PORTG   =  MASK_IO;
    return temp;
}
void updateInput(unsigned char *_input1,unsigned char *_input2, unsigned char *_input3, unsigned char *_input_dipsw)
{
	unsigned char  in1_tmp,in2_tmp,in3_tmp,sw_tmp;
	in1_tmp = in1_buf;
	in2_tmp = in2_buf;
	in3_tmp = in3_buf;
	sw_tmp  = sw_buf;
	in1_buf= readIO(INPUT1);
    in2_buf= readIO(INPUT2);
    in3_buf= readIO(INPUT3);
    sw_buf=  read_dipsw();
	if(in1_buf== in1_tmp) *_input1 = in1_buf;
	if(in2_buf== in2_tmp) *_input2= in2_buf;
	if(in3_buf== in3_tmp) *_input3= in3_buf;
	if(sw_buf== sw_tmp) *_input_dipsw= sw_buf;
}
void updateInput1(unsigned char *_input1,unsigned char *_input2, unsigned char *_input3, unsigned char *_input_dipsw)
{

	*_input1= readIO(INPUT1);
    *_input2= readIO(INPUT2);
    *_input3= readIO(INPUT3);
    *_input_dipsw=  read_dipsw();
	
}

/* read switch disp --------------------- */
unsigned char read_dipsw(){
	unsigned char  temp_switch = 0; 
    if(PING & _BV(SW_3))
      temp_switch  = temp_switch | SW_3_ON;
    if(PING & _BV(SW_2))
       temp_switch = temp_switch | SW_2_ON;
    if(PINB & _BV(SW_1))                          
       temp_switch = temp_switch | SW_1_ON;
    if(PINB & _BV(SW_0)) 
      temp_switch  = temp_switch | SW_0_ON;
    return temp_switch;
}
/* -------------------------------------- */

